package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class PlaceActivity extends MapActivity {
    private static final String TAG = "PlaceActivity";
    private double lat;
    private double lon;
//    private String name;

    private MapView mapView;

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.place);

        Intent intent = getIntent();
        lat = intent.getDoubleExtra("lat", 0);
        lon = intent.getDoubleExtra("lon", 0);
//        name = intent.getStringExtra("name");
        
        Log.i(TAG, "lat: " + lat + "; lon: " + lon);
        mapView = (MapView)findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        
        GeoPoint geoPoint = new GeoPoint((int)(lat * 1e6), (int)(lon * 1e6));
        mapView.getOverlays().add(new PointOverlay(geoPoint));
        mapView.getController().setZoom(15);
        mapView.getController().setCenter(geoPoint);
    }

    @Override
    protected boolean isRouteDisplayed() {
	return false;
    }
    
    private class PointOverlay extends Overlay {
	private GeoPoint geoPoint;
	private Bitmap marker;
	
	public PointOverlay(GeoPoint geoPoint) {
	    this.geoPoint = geoPoint;
	    marker = BitmapFactory.decodeResource(getResources(), R.drawable.place_marker);
	}

        @Override
        public void draw(Canvas canvas, MapView mapView, boolean shadow) {
            super.draw(canvas, mapView, shadow);
            
            Point point = new Point();
            
            Projection projection = mapView.getProjection();
            projection.toPixels(geoPoint, point);
            
            Paint paint = new Paint();
            canvas.drawBitmap(marker, point.x - marker.getWidth() / 2, point.y - marker.getHeight() / 2, paint);
        }
    }
}
